<?php

namespace App\Http\Controllers\Company;

use App\Http\Requests\Company\Login\AjaxCompanyLoginRequest;
use App\Models\Company\CompanyLoginRecord;
use App\Models\Company\CompanyTrialError;
use App\Models\Company\CompanyVisitRecord;
use App\Models\Crm\CrmVisitCompanyRecord;
use Auth,Hash,Site;
use Illuminate\Http\Request;

class LoginController extends CommonController{

    /**
     * ajax验证登录
     * @param AjaxCompanyLoginRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajaxCompanyLogin(Request $request){
        $input = $request->only('username', 'password', 'remember');
        //用户和手机登录多可以
        $remember = !empty($input['remember']) ? $input['remember'] : false;
        if(Auth::guard('company')->attempt(['username' => $input['username'], 'password' => $input['password']], $remember)){

            $user = auth()->guard('company')->user();
            //登录记录表
            $this->loginRecord($user);
            return response()->json(['status' => '0', 'msg' => '登录用户成功!']);
        }else if(Auth::guard('company')->attempt(['mobile' => $input['username'], 'password' => $input['password']], $remember)){
            $user = auth()->guard('company')->user();
            //登录记录表
            $this->loginRecord($user);
            return response()->json(['status' => '0', 'msg' => '登录用户成功!']);
        }else{
            //企业试错登录记录
            $this->trialErrorRecord($input);
            return response()->json(['status' => '1', 'msg' => '用户名或密码错误!']);
        }
    }

    /**
     * 企业试错记录信息表
     * @param $userInfo
     */
    public function trialErrorRecord($input){
        //试错记录表
        $trial = array();
        $trial['username'] = $input['username'];
        $trial['password'] = $input['password'];
        $trial['company_login_ip'] = getIp();
        //登录记录表
        CompanyTrialError::create($trial);
    }

    /**
     * 登录记录信息
     * @param $userInfo
     */
    public function loginRecord($user){
        //登录记录表
        $record = array();
        $record['uid'] = $user->uid;
        $record['company_login_ip'] = getIp();
        $record['company_login_time'] = date('Y-m-d H:i:s', time());
        $record['login_type'] = 2;
        $record['type'] = 1;
        //登录记录表
        CompanyLoginRecord::create($record);
        //更新用户表
        $currtime = date('Y-m-d H:i:s', time());
        $fifedate = date('Y-m-d H:i:s', strtotime($user->surpris_visit_time . "+15 day"));
        if($fifedate < $currtime){
            $user->surpris_visit_time = date('Y-m-d H:i:s', time());
            //15天内插入突访记录表
            CompanyVisitRecord::create($record);
        }
        //设置突访时间
        if(empty($user->surpris_visit_time)){
            $user->surpris_visit_time = date('Y-m-d H:i:s', time());
        }
        $user->last_login_time = date('Y-m-d H:i:s', time());
        $user->last_login_ip = getIp();
        $user->save();
    }

    /**
     * crm链接自动认证登录操作
     * @param Request $request
     * @return mixed
     */
    public function crmLogin(Request $request){
        //CRM链接匹配
        if(!empty($request->crmHash) && strlen(Site::get('CrmLaravelAppKey')) > 30){
            if (Hash::check($request->companyUid.$request->crmUid.Site::get('CrmLaravelAppKey'), $request->crmHash)) {
                //登录操作,如果已有账号登录，先注销再登录
                if(Auth::guard('company')->check()){
                    Auth::guard('company')->logout();
                    //重新登录
                    Auth::guard('company')->loginUsingId($request->companyUid);
                }else{
                    Auth::guard('company')->loginUsingId($request->companyUid);
                }
                //增加一次登录记录
                try{
                    $date['uid'] = $request->companyUid;
                    $date['crm_uid'] = $request->crmUid;
                    $date['company_login_ip'] = getIp();
                    $date['type'] = $request->type;
                    CrmVisitCompanyRecord::create($date);

                }catch (\PDOException $e){
                    abort(503);
                }
                return redirect('company/welcome');
            }
        }
    }

}